<?php
	$realm = $_GET["realm"];
	$name = $_GET["character"];
	$realmType = $_GET["realmType"] ? $_GET["realmType"] : "US";
	$rt = ($realmType == "EU" ? 'eu' : 'www');
	$realm = str_replace(" ","+",$realm);
	$url = curl_init("http://$rt.wowarmory.com/character-sheet.xml?r=".stripslashes($realm)."&n=".stripslashes($name));
	$showProf = isset($_GET["showProf"]) ? $_GET["showProf"] : 1;
	$showAchiev = isset($_GET["showAchiev"]) ? $_GET["showAchiev"] : 1;
	$showGear = isset($_GET["showGear"]) ? $_GET["showGear"] : 1;

	/* -- List of Profession Icons -- */
	$prof_img["Jewelcrafting"] 	=	'inv_misc_gem_02';
	$prof_img["Inscription"] 	=	'inv_inscription_tradeskill01';
	$prof_img["Leatherworking"]	=	'inv_misc_armorkit_17';
	$prof_img["Skinning"]		=	'inv_misc_pelt_wolf_01';
	$prof_img["Herbalism"]		=	'spell_nature_naturetouchgrow';
	$prof_img["Alchemy"]		=	'trade_alchemy';
	$prof_img["Blacksmithing"]	=	'trade_blacksmithing';
	$prof_img["Engineering"]	=	'trade_engineering';
	$prof_img["Enchanting"]		=	'trade_engraving';
	$prof_img["Mining"]			=	'trade_mining';
	$prof_img["Tailoring"]		=	'trade_tailoring';
	/* ------------------------------ */
	
	curl_setopt($url, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
	curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($url);
	curl_close($url);
	$parser = xml_parser_create();
	xml_parse_into_struct($parser, $output,$vals,$index);
	xml_parser_free($parser);

	/* -- Main Block -- */
	$char_info = $vals[$index["CHARACTERINFO"][0]]["attributes"];
	if ($char_info['ERRCODE'] == "noCharacter") {
    echo '<p>Character not Found</p>';
	} else {
    $char_attr = $vals[$index["CHARACTER"][0]]["attributes"];
    echo "<p><img src=\"http://www.wowarmory.com/images/portraits/wow".($char_attr["LEVEL"] < 70 ? '' : ($char_attr["LEVEL"] < 80 ? '-70' : '-80'))."/".$char_attr["GENDERID"]."-".$char_attr["RACEID"]."-".$char_attr["CLASSID"].'.gif" width="48" style="float:left;padding-right:3px;" /><span style="font-weight:bold;font-size:130%;"><a href="'."http://$rt.wowarmory.com/character-sheet.xml?r=".stripslashes($realm)."&n=".stripslashes($name).'">'.($char_attr["PREFIX"] ? $char_attr["PREFIX"].' ' : '').$char_attr["NAME"].($char_attr["SUFFIX"] ? $char_attr["SUFFIX"].' ' : '').'</a></span><br/><span style="font-size:90%;"> of '.$char_attr["REALM"]."-$realmType";
    echo '<br/>Level '.$char_attr["LEVEL"].' '.$char_attr["RACE"].' '.$char_attr["CLASS"].'</span></h3>';
    
    /* -- Gear -- */
    if ($showGear == 1) {
      echo "<h4>Equipment</h4>";
      echo "<ul>";
      foreach($index["ITEM"] as $items) {
        $item = $vals[$items]["attributes"];
        if ($item["SLOT"] >= 0) {
          
          $enchants = '';
          if ($item["PERMANENTENCHANT"] != "0")
            $enchants.="ench=".$item["PERMANENTENCHANT"];
            
          if ($item["RANDOMPROPERTIESID"] != "0")
            $enchants.=(($enchants) ? ';' : '')."rand=".$item["RANDOMPROPERTIESID"];
         
          if ($item["GEM0ID"] != "0" || $item["GEM1ID"] != "0" || $item["GEM1ID"] != "0") {
            $enchants.=(($enchants) ? ';' : '')."gems=";
            if ($item["GEM0ID"] != "0") 
              $enchants.=$item["GEM0ID"];
            if ($item["GEM1ID"] != "0") 
              $enchants.=":".$item["GEM1ID"];
            if ($item["GEM2ID"] != "0") 
              $enchants.=":".$item["GEM2ID"];
          }
          
          echo '<li style="display:inline;background-image:none;padding:0;margin:0;"><a href="http://www.wowhead.com/?item='.$item["ID"].'"'.(($enchants) ? ' rel="'.$enchants.'"' : '').'>';
          echo '<img src="http://www.wowarmory.com/wow-icons/_images/51x51/'.$item["ICON"].'.jpg" width="26" border="0"/>';
          echo '</a></li>';
        }
      }
      echo "</ul>";
    }
    
    /* -- Professions -- */
    if ($showProf==1){
      $prof_1 = $vals[$index["SKILL"][0]]["attributes"];
      $prof_2 = $vals[$index["SKILL"][1]]["attributes"];
      if ($prof_1 != NULL && $prof_2 != NULL) {
        echo "<h4>Professions</h4>";
        echo '<ul style="list-style:none;">';
        if ($prof_1 != NULL) {
          echo '<li style="background-image:none;"><img src="http://static.wowhead.com/images/icons/small/'.$prof_img[$prof_1["NAME"]].'.jpg" width="16" />&nbsp;&nbsp;<span style="font-weight:bold;">'.$prof_1["NAME"].':</span> '.$prof_1["VALUE"].'/'.$prof_1["MAX"].'</li>';
        }
        if ($prof_2 != NULL) {
          echo '<li style="background-image:none;"><img src="http://static.wowhead.com/images/icons/small/'.$prof_img[$prof_2["NAME"]].'.jpg" width="16" />&nbsp;&nbsp;<span style="font-weight:bold;">'.$prof_2["NAME"].':</span> '.$prof_2["VALUE"].'/'.$prof_2["MAX"].'</li>';
          echo '</ul>';
        }
      }
    }
    
    /* -- Achievements -- */
    if ($showAchiev == 1) {
      echo "<h4>Recent Achievements</h4>";
      $url_achiev = curl_init("http://$rt.wowarmory.com/character-achievements.xml?r=".stripslashes($realm).'&n='.stripslashes($name));
      curl_setopt($url_achiev, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
      curl_setopt($url_achiev, CURLOPT_RETURNTRANSFER, true);
      $output_achiev = curl_exec($url_achiev);
      curl_close($url_achiev);
      $parser_achiev = xml_parser_create();
      xml_parse_into_struct($parser_achiev,$output_achiev,$vals_a,$index_a);
      xml_parser_free($parser_achiev);
      echo '<ul style="list-style:none;">';
      foreach($index_a["ACHIEVEMENT"] as $achs) {
        $ach = $vals_a[$achs]["attributes"];
        echo '<li style="display:block;margin:3px;background-image:none;"><a href="http://www.wowhead.com/?achievement='.$ach["ID"].'" ><img src="http://www.wowarmory.com/wow-icons/_images/51x51/'.$ach["ICON"].'.jpg" width="20" border="0" style="padding-right:10px;" /><span style="vertical-align:top;">'.$ach["TITLE"].'</span></a></li>';
      }
      echo '</ul>';
    }
	}
?>